package logic.server;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;

import logic.CircleObject;
import logic.Scene;
import logic.Vec2D;

public class Player extends CircleObject {
	private static final float pRad = 20.0f;
	private String name = "Unnamed player";
	private int team = 0;
	private boolean canShoot = false;
	private Scene parent = null;
	private int shootCooldown = 0;
	private static final int COOLDOWN = 70;
	
	private static final float SHOOT_RANGE = 10.0f;
	private static final float SHOOT_POWAH = 15.0f;
	
	public Scene getParent() {
		return parent;
	}

	public void setParent(Scene parent) {
		this.parent = parent;
	}

	public boolean isCanShoot() {
		return canShoot;
	}

	public void setCanShoot(boolean canShoot) {
		this.canShoot = canShoot;
	}

	private int sprintAmmount = 0;
	public static int SPRINT_MAX = 500;
	
	public Player(String name, int team) {
		super(0, 0, pRad);
		this.name = name;
		this.team = team;
	}

	public void update()
	{
		super.update();
		
		if(sprintAmmount < SPRINT_MAX) sprintAmmount++;
		
		canShoot = pos.distanceTo(parent.getBall().getPosition()) < rad + parent.getBall().getRad() + SHOOT_RANGE;
		if(shootCooldown > 0) { canShoot = false; shootCooldown--; }
		
	}
	
	public int getSprint() { return sprintAmmount; }
	
	public void subSprint() { sprintAmmount -= 15; if (sprintAmmount < 0) sprintAmmount = 0; }
	
	private static final Stroke stroke = new BasicStroke(3.0f); 
	@Override
	public void draw(Graphics2D g) {
		super.draw(g);
		
		if(sprintAmmount > SPRINT_MAX / 2)
			g.setColor(Color.black);
		else
			g.setColor(Color.white);
		
		Stroke oldStroke = g.getStroke();
		g.setStroke(stroke);
		g.drawLine( //(int)(pos.x() - rad),
				(int)(pos.x() - (((float)sprintAmmount / (float)SPRINT_MAX) * (rad))),
				(int)pos.y(),
				(int)(pos.x() + (((float)sprintAmmount / (float)SPRINT_MAX) * (rad))),
				(int)pos.y());
		g.setStroke(oldStroke);
		
		if(canShoot)
		{
			g.setColor(Color.black);
			g.fillOval((int)(pos.x() - 5.0f), (int)(pos.y() - 5.0f), 10, 10);
			
			Vec2D sdir = parent.getBall().getPosition().sub(pos);
			sdir.normalize();
			sdir = sdir.mul(100.0f);
			
			g.setColor(Color.black);
			g.drawLine((int)pos.x(), (int)pos.y(), (int)(pos.x() + sdir.x()), (int)(pos.y() + sdir.y()));
		}
		
		if(shootCooldown > 0)
		{
			g.setColor(Color.white);
			g.drawOval((int)(pos.x() - rad), (int)(pos.y() - rad), (int)(rad * 2), (int)(rad * 2));
		}
	}
	
	public void shoot()
	{
		Vec2D sdir = parent.getBall().getPosition().sub(pos);
		sdir.normalize();
		sdir = sdir.mul(SHOOT_POWAH);
		parent.getBall().applyForce(sdir);
		shootCooldown = COOLDOWN;
	}
}
